/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BL;

import Bean.BaiViet;
import Util.DBConnection;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;


public class BLBaiViet {
    public int InsertBaiViet(int MaBViet, String TieuDe, String GioiThieu, String NoiDung, String HinhAnh, String Tag,Date NgayViet, int SLXem)
    {
        int rs=0;
        try{
            Connection conn=null;
            conn=DBConnection.getConnection();
            String sql="INSERT INTO baiviet(MABAIVIET, TIEUDE, GIOITHIEUBAIVIET, NOIDUNG, HINHANHDD, TAGBAIVIET, NGAYVIET, SOLUONGXEM) VALUES (?,?,?,?,?,?,?,?)";
            PreparedStatement preStm=(PreparedStatement) conn.prepareStatement(sql);
            preStm.setInt(1,MaBViet );
            preStm.setString(2,TieuDe );
            preStm.setString(3,GioiThieu );
            preStm.setString(4,NoiDung );
            preStm.setString(5,HinhAnh );
            preStm.setString(6,Tag );
            preStm.setDate(7,NgayViet );
            preStm.setInt(8,SLXem );
            rs=preStm.executeUpdate();
            conn.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }
        
        return rs;
    }
    public int UpdateBaiViet(int MaBViet, String TieuDe, String GioiThieu, String NoiDung, String HinhAnh, String Tag,Date NgayViet, int SLXem){
        int rs=0;
        try{
            Connection conn=null;
            conn=DBConnection.getConnection();
            String sql="UPDATE baiviet SET TIEUDE=?,GIOITHIEUBAIVIET=?,NOIDUNG=?,HINHANHDD=?,TAGBAIVIET=?,NGAYVIET=?,SOLUONGXEM=? WHERE MABAIVIET=?";
            PreparedStatement preStm=(PreparedStatement) conn.prepareStatement(sql);
            preStm.setString(1,TieuDe );
            preStm.setString(2,GioiThieu );
            preStm.setString(3,NoiDung );
            preStm.setString(4,HinhAnh );
            preStm.setString(5,Tag );
            preStm.setDate(6,NgayViet );
            preStm.setInt(7,SLXem );
            preStm.setInt(8,MaBViet );
            rs=preStm.executeUpdate();
            conn.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }
        
        return rs;
    }
    public BaiViet getBaiViet(int MaBViet) throws SQLException
    {
        Connection conn=null;
        conn=DBConnection.getConnection();
        Statement stm=(Statement) conn.createStatement();
        String sql="SELECT * FROM baiviet WHERE MABAIVIET="+MaBViet;
        ResultSet rs=stm.executeQuery(sql);
        BaiViet BV=new BaiViet();
        if(rs.next()){
//            MABAIVIET, TIEUDE, GIOITHIEUBAIVIET, NOIDUNG, HINHANHDD, TAGBAIVIET, NGAYVIET, SOLUONGXEM
            BV.setMaBaiViet(rs.getInt("MABAIVIET"));
            BV.setTieuDe(rs.getString("TIEUDE"));
            BV.setGioiThieuBV(rs.getString("GIOITHIEUBAIVIET"));
            BV.setNoiDung(rs.getString("NOIDUNG"));
            //NgDung.setHinhDaiDien(HINHANHDD);
            BV.setTag(rs.getString("TAGBAIVIET"));
            BV.setNgayViet(rs.getDate("NGAYVIET"));
            BV.setSLXem(rs.getInt("SOLUONGXEM"));
        }
        conn.close();
        return BV;
    }
}
